<?php
namespace Api\Controller;
use Think\Controller;
/**
 * Class IndexController
 * @package Api\Controller
 *
 */
class IndexController extends ApiBasicController{
    public function _initialize(){
        parent::_initialize();
        //JSON_UNESCAPED_UNICODE
    }

    /**
     * 首页展示
     * 传参方式 GET
     * @author mss
     * @time 2017-12-14
     * @param prefix_name 域名前缀
     */
    public function index(){
        if(empty($_GET['prefix_name'])){
            apiResponse('error','参数错误');
        }
        //查询商家基本信息
        $name = $_GET['prefix_name'];
        $where['prefix_name'] = $name;
        $where['status'] = ['NEQ',9];
        $shop = M('Shop')->where($where)->find();
        $data['shop_name'] = $shop['name'];
        $data['head_pic'] = 'https://'.$_SERVER['HTTP_HOST'].'/'.$shop['head_pic'];
        if(!empty($shop['pic'])){
            $pics = explode(',',$shop['pic']);
            foreach($pics as $pic_k=>$pic_v){
                $pics[$pic_k] = 'https://'.$_SERVER['HTTP_HOST'].'/'.$pic_v;
            }
            $data['pic_list'] = $pics;
        }else{
            $data['pic_list'] = array();
        }
        $content = $shop['content'];
        preg_match_all('/src=\"\/?(.*?)\"/',$content,$match);
        foreach($match[1] as $key => $src){
            if(!strpos($src,'://')){
                $content = str_replace('/'.$src,'https://'.$_SERVER['HTTP_HOST']."/".$src,$content);
            }
        }
        $data['content'] = $content;
        $data['shop_tel'] = $shop['tel'];
        $data['address'] = $shop['address'];
        $data['image_text'] = $shop['image_text'];
        $w['shop_id'] = $shop['shop_id'];
        $w['status'] = array('NEQ',9);
        $news = M('News')->where($w)->field('news_id,title,ctime')->order('sort DESC,ctime DESC')->limit(5)->select();
        if($news){
            foreach($news as $k=>$v){
                $news[$k]['time'] = date('Y-m-d',$v['ctime']);
            }
        }else{
            $news = array();
        }
        $data['news'] = $news;
        $article = M('Article')->where($w)->field('art_id,title,pic,desc,ctime')->order('sort DESC,ctime DESC')->limit(5)->select();
        if($article){
            foreach($article as $key=>$val){
                $article[$key]['pic'] = 'https://'.$_SERVER['HTTP_HOST'].'/'.$val['pic'];
                $article[$key]['time'] = date('Y-m-d',$val['ctime']);
            }
        }else{
            $article = array();
        }
        $data['article'] = $article;
        apiResponse('success','获取成功',$data);
    }

    /**
     * 文章列表
     * 传参方式 get
     * @author mss
     * @time 2017-12-14
     * @param prefix_name 域名前缀
     * @param p 分页页数
     */
    public function articleList(){
        if(empty($_GET['prefix_name'])){
            apiResponse('error','参数错误');
        }
        $where['status'] = ['NEQ',9];
        $where['prefix_name'] = $_GET['prefix_name'];
        $shop_id = M('Shop')->where($where)->getField('shop_id');
        if(!$shop_id){
            apiResponse('error','域名不存在');
        }
        $p = $_GET['p']?$_GET['p']:1;
        $data = array();
        $w['shop_id'] = $shop_id;
        $w['status'] = 1;
        $page = ($p-1)*10;
        $list = M('Article')->where($w)->field('art_id,title,pic,desc,ctime')->order('sort DESC,ctime DESC')->limit($page,10)->select();
        if(!$list){
            apiResponse('success','暂无数据',$data);
        }
        foreach($list as $k=>$v){
            $data[$k]['art_id'] = $v['art_id'];
            $data[$k]['title'] = $v['title'];
            $data[$k]['pic'] = 'https://'.$_SERVER['HTTP_HOST'].'/'.$v['pic'];
            $data[$k]['desc'] = $v['desc'];
            $data[$k]['time'] = date('Y-m-d',$v['ctime']);
        }
        apiResponse('success','成功',$data);
    }

    /**
     * 文章详情
     * 传参方式 get
     * @author mss
     * @time 2017-12-14
     * @param art_id 文章id
     */
    public function articleDetail(){
        $art_id = $_GET['art_id'];
        if(!$art_id){
            apiResponse('error','参数错误');
        }
        $w['art_id'] = $art_id;
        $info = M('Article')->where($w)->field('art_id,shop_id,title,pic,desc,content,ctime,status')->find();
        if($info['status']==9||!$info){
            apiResponse('error','文章不存在');
        }
        $data['art_id'] = $info['art_id'];
        $data['pic'] = 'https://'.$_SERVER['HTTP_HOST'].'/'.$info['pic'];
        $data['title'] = $info['title'];
        $data['time'] = date('Y-m-d',$info['ctime']);
        $data['desc'] = $info['desc'];
        $content = $info['content'];
        preg_match_all('/src=\"\/?(.*?)\"/',$content,$match);
        foreach($match[1] as $key => $src){
            if(!strpos($src,'://')){
                $content = str_replace('/'.$src,'https://'.$_SERVER['HTTP_HOST']."/".$src,$content);
            }
        }
        $data['content'] = $content;
//        $data['content'] = $info['content'];
        M('Article')->where($w)->setInc('click_num',1);

        //查询其他文章
        $others = array();
        $other_list = M('Article')->where(array('art_id'=>array('NEQ',$art_id),'shop_id'=>$info['shop_id']))->field('art_id,title,pic,desc')->order('ctime DESC,click_num DESC')->limit(5)->select();
        if($other_list){
            foreach($other_list as $k=>$v){
                $others[$k]['art_id'] = $v['art_id'];
                $others[$k]['title'] = $v['title'];
                $others[$k]['pic'] = 'https://'.$_SERVER['HTTP_HOST'].'/'.$v['pic'];
                $others[$k]['desc'] = $v['desc'];
            }
        }
        $data['others'] = $others;
        apiResponse('success','成功',$data);
    }

    /**
     * 新闻列表
     * 传参方式 get
     * @author mss
     * @time 2017-12-14
     * @param prefix_name 域名前缀
     * @param p 分页页数
     */
    public function newsList(){
        if(empty($_GET['prefix_name'])){
            apiResponse('error','参数错误');
        }
        $where['status'] = ['NEQ',9];
        $where['prefix_name'] = $_GET['prefix_name'];
        $shop_id = M('Shop')->where($where)->getField('shop_id');
        if(!$shop_id){
            apiResponse('error','域名不存在');
        }
        $p = $_GET['p']?$_GET['p']:1;
        $data = array();
        $w['shop_id'] = $shop_id;
        $w['status'] = 1;
        $page = ($p-1)*10;
        $list = M('News')->where($w)->field('news_id,title,ctime')->order('sort DESC,ctime DESC')->limit($page,10)->select();
        if(!$list){
            apiResponse('success','暂无数据',$data);
        }
        foreach($list as $k=>$v){
            $data[$k]['news_id'] = $v['news_id'];
            $data[$k]['title'] = $v['title'];
            $data[$k]['time'] = date('Y-m-d',$v['ctime']);
        }
        apiResponse('success','成功',$data);
    }

    /**
     * 新闻详情
     * 传参方式 get
     * @author mss
     * @time 2017-12-14
     * @param news_id 新闻id
     */
    public function newsDetail(){
        $news_id = $_GET['news_id'];
        if(!$news_id){
            apiResponse('error','参数错误');
        }
        $w['news_id'] = $news_id;
        $info = M('News')->where($w)->field('news_id,title,content,ctime,status')->find();
        if($info['status']==9||!$info){
            apiResponse('error','文章不存在');
        }
        $data['news_id'] = $info['news_id'];
        $data['title'] = $info['title'];
        $data['time'] = date('Y-m-d',$info['ctime']);
        $data['content'] = $info['content'];
        M('News')->where($w)->setInc('click_num',1);
        apiResponse('success','成功',$data);
    }

    /**
     * 用户留言
     * 传参方式 post
     * @author mss
     * @time 2017-12-14
     * @param prefix_name 商家id
     * @param name 用户姓名
     * @param phone 手机号码
     * @param content 留言内容
     */
    public function addMessage(){
        if(empty($_POST['prefix_name'])||empty($_POST['name'])||empty($_POST['phone'])||empty($_POST['content'])){
            apiResponse('error','参数错误');
        }
        $w['prefix_name'] = $_POST['prefix_name'];
        $w['status'] = ['NEQ',9];
        $shop_id = M('Shop')->where($w)->getField('shop_id');
        if(!$shop_id){
            apiResponse('error','域名不存在');
        }
        $phone = $_POST['phone'];

        $data['shop_id'] = $shop_id;
        $data['name'] = $_POST['name'];
        $data['phone'] = $phone;
        if (get_magic_quotes_gpc()) {
            $data['content'] = stripslashes($_POST['content']);
        } else {
            $data['content'] = $_POST['content'];
        }
        $data['ctime'] = time();
        $res = M('Message')->add($data);
        if($res){
            apiResponse('success','留言成功');
        }else{
            apiResponse('error','留言失败');
        }
    }

    /**
     * 商家相册列表
     * 传参方式 get
     * @author mss
     * @time 2017-12-14
     * @param prefix_name 域名前缀
     * @param p 分页页数
     */
    public function album(){
        if(empty($_GET['prefix_name'])){
            apiResponse('error','参数错误');
        }
        $p = $_GET['p']?$_GET['p']:1;
        $page = ($p-1)*10;
        $w['prefix_name'] = $_GET['prefix_name'];
        $w['status'] = 0;
        $shop_id = M('Shop')->where($w)->getField('shop_id');
        if(!$shop_id){
            apiResponse('域名不存在');
        }
        $where['shop_id'] = $shop_id;
        $where['status'] = 1;
        $data = array();
        $list = M('Picture')->where($where)->limit($page,10)->field('pic_id,pic')->order('sort DESC,ctime DESC')->select();
        if(empty($list)){
            apiResponse('success','暂无数据',$data);
        }
        foreach($list as $k=>$v){
            $data[$k]['pic_id'] = $v['pic_id'];
            $data[$k]['pic'] = 'https://'.$_SERVER['HTTP_HOST'].'/'.$v['pic'];
        }
        apiResponse('success','成功',$data);
    }

}